//
//  DeviceCell.m
//  DYLYJ
//
//  Created by JM on 15/10/24.
//  Copyright © 2015年 JM. All rights reserved.
//

#import "DeviceCell.h"
#import "DYUIKit.h"

#import "AppDelegate.h"


@interface DeviceCell () //<XXNibBridge> // Enable nib bridge

@end
//使用registerNib 在实例化cell时会触发initWithCoder ,awakeFromNib. 使用registerClass 它应该会走initWithStyle

//- (id)initWithCoder:(NSCoder *)aDecoder {
//    if (self = [super initWithCoder:aDecoder]) {
//        [self shareInit];
//    }
//    return self;
//}

@implementation DeviceCell



//- (id)initWithCoder:(NSCoder *)aDecoder {
//    self = [DeviceCell cell];
////    if (self = [super initWithCoder:aDecoder]) {
////       
////    }
//    return self;
//}
//
//
//- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
////    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"DeviceCell" owner:nil options:nil] ;
////    self = [nibs firstObject];
//    
//    self = [DeviceCell cell];
////    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
////       
////    }
//    return self;
//}

+ (instancetype)cell {
//    UINib * nib = [UINib nibWithNibName:[DeviceCell cell] bundle:nil];
    
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"DeviceCell" owner:nil options:nil] ;
    DeviceCell * cell = [nibs firstObject];
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.tintColor = [UIColor clearColor];
    cell.stateLabel.adjustsFontSizeToFitWidth = YES;
    cell.nameLabel.adjustsFontSizeToFitWidth = YES;
//    cell.contentView
    return cell;
}

//----------------
+ (NSString *)ID {
    return @"DeviceCell";
}

+ (NSInteger)height {
    return 90;
}
- (CGFloat)height {
    return 90;
}

- (void)awakeFromNib {
    NSLog(@"ddd");
    if ((!self.iconView) && (!self.nameLabel)) {
        DeviceCell * cell = [[[NSBundle mainBundle] loadNibNamed: NSStringFromClass([self class]) owner:self options:nil] firstObject];
        
        cell.translatesAutoresizingMaskIntoConstraints = NO;
        self.translatesAutoresizingMaskIntoConstraints = NO;
    
        [self.contentView addSubview: cell.contentView];
        self.iconView = cell.iconView;
        self.nameLabel = cell.nameLabel;
        self.stateLabel = cell.stateLabel;
        self.openSwitch = cell.openSwitch;
    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setDevice:(Device *)device; {
    _device = device;
    
//    self.upButton.selected = _device.isClose_On;
//    self.downButton.selected = _device.isOpen_On;
//    self.stopButton.selected = _device.isStop_On;
//    self.lightButton.selected = _device.isLED_On;
//    self.dryButton.selected = _device.isHonggan_On;
//    self.airDryButton.selected = _device.isFenggan_On;
//    self.xiaoduButton.selected = _device.isXiaodu_On;
    
    
    self.iconView.image = [UIImage imageNamed:device.iconName];
    self.nameLabel.text = device.name;
    self.openSwitch.on = device.isOpen;
    self.openSwitch.onTintColor = UIColorFromHexStr(device.switchColor);
    self.stateLabel.text = _device.state;
    
    if (myAppDelegate.isDemo){
        //todo bug
         self.openSwitch.on = device.isPower_on;
        
        return;
    }
    
    if (device.xpgDevice.isOnline){
        if (device.xpgDevice.isConnected && device.haveReceiveDataState){
            self.stateLabel.text = _device.state;
        }else{
            if (device.xpgDevice.isLAN){
                self.stateLabel.text = @"本地在线";
            }else{
                self.stateLabel.text = @"远程在线";
            }
        }
    }else{
        self.stateLabel.text = @"离线";
    }
   
    
}
- (void) update {
    [self setDevice:_device] ;
}


+ (NSString *)xib; {
    return @"DeviceCell";
}
//-(void)willTransitionToState:(UITableViewCellStateMask)state{
//    [super willTransitionToState:state];
//}
//-(void)didTransitionToState:(UITableViewCellStateMask)state{
//    [super didTransitionToState:state];
//    
////    //滑动出现的删除按钮state是2的，编辑状态下的删除按钮state是3的
////    if (state == UITableViewCellStateShowingDeleteConfirmationMask||state==3) {
////        for (UIView *subview in self.subviews) {
////            //cell的subview为UITableViewCellDeleteConfirmationControl时，代表是删除按钮
////            if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
////                 subview.backgroundColor = [UIColor clearColor];
////                UIView *deleteButtonView = subview;
////                CGRect f = deleteButtonView.frame;
////                f.origin.x -= 50;
////                deleteButtonView.frame = f;            }
////        }
////    }
////    //插入和移动的编辑状态state都是1
////    else
////        if(state==UITableViewCellStateShowingEditControlMask)
//        {
//        for (UIView *subview in self.subviews) {
//            
//            self.backgroundColor = [UIColor clearColor];
//            NSString* type = @"";
//            //判断如果cell当前是插入模式，则寻找UITableViewCellEditControl的subview，代表添加按钮
//            if (self.editingStyle==UITableViewCellEditingStyleInsert) {
//                type = @"UITableViewCellEditControl";
//            }
//            //否则寻找UITableViewCellReorderControl的subview，代表移动按钮
//            else type = @"UITableViewCellReorderControl";
//            if ([NSStringFromClass([subview class]) isEqualToString:type]) {
//                
//                
//                subview.backgroundColor = [UIColor clearColor];
//                UIView *deleteButtonView = subview;//[subview.subviews objectAtIndex:0];
//                CGRect f = deleteButtonView.frame;
//                f.origin.x -= 50;
//                deleteButtonView.frame = f;
//            }
//        }
//        
//    }
//}
- (IBAction)swichValueChange:(UISwitch*)sender {
    if ([self.delegate respondsToSelector:@selector(didDeviceClickCmd:)]){
        if (self.device.isPower_on)  {
            [self.delegate didDeviceClickCmd:DevieClickCmdPowerOn];
        } else {
            [self.delegate didDeviceClickCmd:DevieClickCmdPowerOff];
        }
    }
}

//- (void)didReceiveMessage:(id)message withtype:(NSString *)aSMSType {
//    Device *source = message;
//    if ([_device.macAddress isEqualToString:source.macAddress]){
//        [self setDevice:source];
//    }
//}

@end
